<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.UserDAO"%>
<%@page import="db.TitleDAO" %>
<%@page import="db.User"%>
<%@page import="db.GroupDAO"%>


<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	UserDAO userDao = (UserDAO)ctx.getBean("UserDAO");
	TitleDAO titleDao = (TitleDAO)ctx.getBean("TitleDAO");
	GroupDAO groupDao = (GroupDAO)ctx.getBean("GroupDAO");
	request.setCharacterEncoding("utf-8");
	String uid = request.getParameter("uid");
	int id = 0;
	if(uid!=null || !"".equals(uid)){
		id = Integer.parseInt(uid);
	}
	String message = "";
	
	String type = request.getParameter("type");
	//type=2提交表单
	if(type!=null && (type=="2" || "2".equalsIgnoreCase(type))){
		//System.out.println("提交");
		request.setAttribute("type",2);
		String stitle = request.getParameter("stitle");
		String sgroup = request.getParameter("sgroup");
		int stitleint = 0;
		int sgroupint = 0;
		if(stitle!=null && sgroup!=null){
			stitleint = Integer.parseInt(stitle);
			sgroupint = Integer.parseInt(sgroup);
		}
		String account = request.getParameter("account");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String remarks = request.getParameter("remarks");
		User user = new User();
		user = userDao.findById(id);
		//判断登录名是否为空，是否重复
		if(account==null || "".equals(account)){
			message = "修改管理员失败，管理员登录名不能为空。";
			request.setAttribute("message",message);
			request.setAttribute("suess",false);
			//System.out.println(message+"========"+stitleint+"========="+sgroupint+"=====type==="+type);
			pageContext.forward("/vm/erp_tpl/modAdmin.vm");
			return;
		}else{
			if(account == user.getAccount() || account.equals(user.getAccount())){
				message = "管理员名称未做修改！";
			}else{
				List list = userDao.findByAccount(account);
				if(list != null && list.size() > 0 ){
					message = "添加管理员失败，管理员登录名已经存在。";
					request.setAttribute("suess",false);
					request.setAttribute("message",message);
					pageContext.forward("/vm/erp_tpl/modAdmin.vm");
					return;
				}
			}
		}
		//用户密码是否为空
		if(password==null || "".equals(password)){
			message = "修改管理员失败，管理员密码不能为空。";
			request.setAttribute("message",message);
			request.setAttribute("suess",false);
			pageContext.forward("/vm/erp_tpl/modAdmin.vm");
			return;
		}else if(password.length()<6){
			message = "修改管理员失败，管理员密码不足6位。";
			request.setAttribute("message",message);
			request.setAttribute("suess",false);
			pageContext.forward("/vm/erp_tpl/modAdmin.vm");
			return;
		}
		//姓名不可为空
		if(name==null || "".equals(name)){
			message = "修改管理员失败，管理员姓名不能为空。";
			request.setAttribute("message",message);
			request.setAttribute("suess",false);
			pageContext.forward("/vm/erp_tpl/modAdmin.vm");
			return;
		}
		
		
		user.setTitleId(stitleint);
		user.setGroupId(sgroupint);
		user.setPassword(password);
		user.setAccount(account);
		user.setRemarks(remarks);
		System.out.println(remarks);
		user.setName(name);
		user.setDate(new Date());
		userDao.attachDirty(user);
		int nuid = user.getId();
		//System.out.println(uid);
		message = "恭喜您！修改管理员成功！";
		request.setAttribute("message",message);
		request.setAttribute("suess",true);
		request.setAttribute("uid",nuid);
		pageContext.forward("/vm/erp_tpl/modAdmin.vm");
		return;
		
		
	}else{
	//正常显示
		//System.out.println("显示");
		User user = new User();
		user = userDao.findById(id);
		List listtitleDao = titleDao.findAll();
		List listgroupDao = groupDao.findAll();
		request.setAttribute("user",user);
		request.setAttribute("titleDao",listtitleDao);
		request.setAttribute("groupDao",listgroupDao);
		request.setAttribute("type",1);
		request.setAttribute("uid",id);
		pageContext.forward("/vm/erp_tpl/modAdmin.vm");
		
		
		//userDao.getMaterials(0, 0, new String[][]{new String[]{"desc","createDate"}}, new Object[][]{new Object[]{"eq","userId",userId},new Object[]{"eq","folderId",obj[0]},new Object[]{"eq","state","1"}}, new String[][]{new String[]{"","id"},new String[]{"","tag"},new String[]{"","MType"},new String[]{"","UType"},new String[]{"","t.createDate"},new String[]{"","path"}}, new String[]{"title","grope"});
	}
	
%>
